/**
 * Created by jy on 2014/7/30.
 */

var sendAuto = require(__dirname+'/HttpHandler.js').sendAuto;
var errorMap = require(__dirname +'/../config/ErrorCode.js').ERRORMAP;
var utils = require(__dirname + "/utils.js");
var _ = require('underscore');
var Q = require('q');
var moment = require('moment');

exports.setOptions = function(options){
  console.log('load weixin');
};

exports.logWeixin = function(req,res){
  //get方式的参数为signature，timestamp，nonce，echostr，验证后返回echostr
  if(req.method === 'GET'){
    console.log('weixin get request');
    var param = _.extend(req.query);
    console.log(param);
    var checkParam = {
      timestamp:param.timestamp,
      nonce:param.nonce,
      signature:param.signature
    };
    if(utils.weixinSign(checkParam)){
      sendAuto(req,res,param.echostr);
    }
    sendAuto(req,res,null);
  }else{
    console.log('weixin post request');
    var param = _.extend(req.body);
    console.log(req.body);
    sendAuto(req,res,param);
  }

};
